//
//  DataManager.m
//  IPTV
//
//  Created by Echo on 13-12-5.
//  Copyright (c) 2013年 游强. All rights reserved.
//

#import "DataManager.h"
#import "NSDataInfo.h"
#import "PokerControlClient.h"

//#define AllDevices  @"AllDevices"


@implementation DataManager
@synthesize deviceArray, devicePath, deviceName;
- (id)initWithName:(NSString*)name{
	if ((self = [super init])) {
        self.deviceName = name;
        self.deviceArray = [[NSMutableArray alloc] init];
        self.devicePath = [DataManager storePathWithFileName:self.deviceName];
        [self getAllDevices];
	}
	return self;
}
-(void)deleteAllDevices{
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:@"AllDevices.plist"];
    if ( [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        [self.deviceArray removeAllObjects];
	}
}
-(void)getAllDevices{
    if ([[NSFileManager defaultManager] fileExistsAtPath:self.devicePath]) {
        [self.deviceArray removeAllObjects];
        NSArray *array =  [DataManager unarchiverObjectFromPath:self.devicePath key:self.deviceName];
        if (array==nil && ([self.deviceName hasPrefix:@"R"] || [self.deviceName hasPrefix:@"S"])) {
            NSString *sub = [self.deviceName substringFromIndex:1];
            array = [DataManager unarchiverObjectFromPath:self.devicePath key:sub];
        }
        [self.deviceArray addObjectsFromArray:array];
//        if (self.deviceArray.count > 0) {
//            int i  = 0;
//            DeviceInfo *deviceInfo = [self.deviceArray objectAtIndex:0];
//            for (NSDataInfo *dataInfo in deviceInfo.keyArray) {
//                NSLog(@"afterxx %d :  %d", i,  dataInfo.isStudy);
//                i ++;
//            }
//        }
        
    }
}
-(void)setAllDevices{
    if (self.deviceArray.count > 0) {
        int i  = 0;
        DeviceInfo *deviceInfo = [self.deviceArray objectAtIndex:0];
        for (NSDataInfo *dataInfo in deviceInfo.keyArray) {
            NSLog(@"before  %d :  %d %@ %@", i,  dataInfo.isStudy, dataInfo.app_cmd, dataInfo.name);
            i ++;
        }
    }
    [DataManager archiverObject:self.deviceArray path:self.devicePath key:self.deviceName];
}
-(void)releaseDataManager:(NSString*)devicename{
    [[DataManager GetInstance:devicename] release];
}
-(void)dealloc{
    [super dealloc];
    managerShare = nil;
    [self.deviceArray release];
    NSLog(@"DataManager dealloc");
}
static DataManager *managerShare = nil;
+ (DataManager *)GetInstance:(NSString*)devName{
    @synchronized(self)     {
		if (!managerShare)
            managerShare = [[DataManager alloc] initWithName:devName];
	}
	return managerShare;
}
-(void)deleteDevice{
    if ( [[NSFileManager defaultManager] fileExistsAtPath:self.devicePath]) {
        NSLog(@"delete : %@", self.devicePath);
		NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:self.devicePath error:&error];
        [self.deviceArray removeAllObjects];
	}
}

NSString* myDocumentsDirectory() {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	return [paths objectAtIndex:0];;
}
+(NSString*)storePathWithFileName:(NSString*)fileName {
    if ([fileName length]==0) {
        return nil;
    }
    NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    if ( ! [[NSFileManager defaultManager] fileExistsAtPath:path]) {
		[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
	}
	NSString *filePath=[path stringByAppendingPathComponent:fileName];
	return filePath;
}

+(void)archiverObject:(id)object path:(NSString*)path key:(NSString*)key{
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:object forKey:key];
    [archiver finishEncoding];
    [data writeToFile:path atomically:YES];
}
+(id)unarchiverObjectFromPath:(NSString*)path key:(NSString*)key{
    NSData *data = [[NSMutableData alloc]
                     initWithContentsOfFile:path];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
                                      initForReadingWithData:data];
    id object = [unarchiver decodeObjectForKey:key];
    [unarchiver finishDecoding];
    return object;
}
-(void)btnPressed{
//    NSString *path = [DataManager storePathWithFileName:@"TV.dat"];
//    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
//        NSArray *array =  [DataManager unarchiverObjectFromPath:path key:@"TV"];
//    }
}
-(void)save{
    NSArray *array = nil;
    NSString *path = [DataManager storePathWithFileName:@"TV.dat"];
    [DataManager archiverObject:array path:path key:@"TV"];
    
}


@end
